package test3.main;

import test3.bean.Employee;
import test3.dao.impl.EmployeeDaoImpl;
import test3.main.Index;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class QueryOneEmployeeAction implements ActionListener {

	Index index;
	
	public QueryOneEmployeeAction(Index in) {
		index = in;
	}

	private EmployeeDaoImpl employeeDao=EmployeeDaoImpl.createEmployeeDao();
	
	@Override
	public void actionPerformed(ActionEvent e) {

		String select = e.getActionCommand();
		if(select == "submit") {

			Integer id= Integer.parseInt(index.queryOneEmployeeIdText.getText());

			Employee employee = employeeDao.queryById(id);

			//当查询无结果时，提示查询失败
			if (employee==null)
				JOptionPane.showMessageDialog(null, "Query failure","Result",JOptionPane.PLAIN_MESSAGE);

			System.out.println(employee);
			//对数据的现实操作

			index.queryOneEmployeeIdText.setText(""+employee.getId());
			index.queryOneEmployeeNameText.setText(employee.getName());
			index.queryOneEmployeeAgeText.setText(""+employee.getAge());
			if (employee.getSex()==0)
			    index.queryOneEmployeeGenderText.setText("女");
			else
				index.queryOneEmployeeGenderText.setText("男");

			Toolkit.getDefaultToolkit().beep();

		}
		else {
			index.card.show(index.panel, "welcome");
		}
	}

}
